﻿using System;
using System.Windows.Input;
using DDU.DataModel;
using DDU.Library;
using System.Linq;
using System.Windows;

namespace DDU.ViewModel
{
    public class FictionReadViewModel : ViewModelBase
    {
        private string fileName;
        public string FileName
        {
            get { return fileName; }
            set
            {
                fileName = value;
                RaisePropertyChanged(() => FileName);
                if (!string.IsNullOrWhiteSpace(FileName))
                {
                    Fiction = new FictionInfo(FileName);
                    RaisePropertyChanged(() => Fiction);
                    RaisePropertyChanged(() => NextCommand);
                    RaisePropertyChanged(() => PrevCommand);
                }
                
            }
        }

        private Int64 currentIndex = 0;
        public Int64 CurrentIndex
        {
            get { return currentIndex; }
            set
            {
                currentIndex = value;
                RaisePropertyChanged(() => CurrentIndex);
                RaisePropertyChanged(() => NextCommand);
                RaisePropertyChanged(() => PrevCommand);
            }
        }

        private Visibility coverVisibility = Visibility.Visible;
        public Visibility CoverVisibility
        {
            get { return coverVisibility; }
            set
            {
                coverVisibility = value;
                RaisePropertyChanged(() => CoverVisibility);
            }
        }

        private FictionInfo fiction;
        public FictionInfo Fiction
        {
            get { return fiction; }
            set
            {
                fiction = value;
                RaisePropertyChanged(() => Fiction);
            }
        }

        public ICommand NextCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Fiction != null && CurrentIndex < Fiction.TotalPages - 1; },
                    ExecuteCallback=delegate{
                        CurrentIndex += 1;
                    }
                };
            }
        }

        public ICommand PrevCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Fiction != null && CurrentIndex > 0; },
                    ExecuteCallback = delegate
                    {
                        CurrentIndex -= 1;
                    }
                };
            }
        }

        public ICommand StartCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        CoverVisibility = Visibility.Collapsed;
                    }
                };
            }
        }
    }
}
